// pages/order/changeProduct/changeProduct.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device_old: '',//旧缴费器id
    device_no: '',//旧缴费器号
    user_name: '',//用户姓名
    user_tel: '',//用户电话

    device_new: '',//新缴费器号
    product: '',//新缴费器下发产品id
    product_name: '',
    product_type: '',//新缴费器下发产品类型
    package: '',//新缴费器下发套餐id
    package_name: '',
    note: '',//备注
    info: [],//工单信息
    equipmentsList: [],//产品列表
    packageList: [],//套餐
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      work_order_id: options.work_order_id
    });
    that.getProduct();
    that.getWorkInfo();

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.getWorkInfo();
  },

  //选择缴费器
  // navDeviceList() {
  //   wx.navigateTo({
  //     url: '/pages/subpackage/pages/device/allDeviceList/allDeviceList',
  //   })
  // },
  //根据工单获取缴费器信息
  getWorkInfo(){
    var that = this;
    var work_order_id = that.data.work_order_id;
    app.ajax({
      url: 'Engineer/Workorder/workOrderDetail',
      data: {
        work_order_id: work_order_id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            info: res.data.data
          })
        }

      }
    })

  },

  //获取产品
  getProduct() {
    var that = this;
    app.ajax({
      url: 'Engineer/Equipments/getList',
      data: {
        pageSize: 9999
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            equipmentsList: res.data.data.data
          })
        }
      }
    })
  },
  //选择新产品
  bindProduct(e) {
    var product_id = this.data.equipmentsList[e.detail.value].equipments_id;
    var product_name = this.data.equipmentsList[e.detail.value].equipments_name;
    var product_type = this.data.equipmentsList[e.detail.value].equipments_type;
    this.setData({
      product: product_id,
      product_name: product_name,
      product_type: product_type,
      packageList: [],
      package: '',//新缴费器下发套餐id
      package_name: '',
    })
    this.getPackage(product_id);
    this.getProduct();
  },

  //获取套餐
  getPackage(id) {
    var that = this;
    app.ajax({
      url: 'Engineer/Equipmentlists/productToPackage',
      data: {
        id: id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            packageList: res.data.data
          })
        }
      }
    })
  },

  //选择套餐
  bindPackage(e) {
    var package_id = this.data.packageList[e.detail.value].package_id;
    var package_name = this.data.packageList[e.detail.value].package_name;
    this.setData({
      package: package_id,
      package_name: package_name,
    })
  },

  //新缴费器号输入
  devicenewInput(e) {
    this.setData({
      device_new: e.detail.value
    })
  },
  //校验缴费器编号
  checkDevice() {
    var that = this;
    if (!that.data.device_new) {
      app.showToast('请输入新缴费器号');
      return
    }
    app.ajax({
      url: 'Engineer/Equipmentlists/checkDevice',
      data: {
        device_no: that.data.device_new
      },
      success: function (res) {
        app.showToast(res.data.message);
      }
    })
  },

  //提交的表单数据
  formSubmit(e) {
    console.log('提交的数据', e)
    var that = this;
    var value = e.detail.value;
    if (!that.data.product) {
      app.showToast('请选择新产品');
      return
    } else if (that.data.product_type == 1 && !that.data.package) {
      app.showToast('请选择套餐');
      return
    }
    app.ajax({
      url: 'Engineer/Equipments/doReplaceProduct',
      data: {
        work_order_id: that.data.work_order_id,
        device_one: that.data.device_old,
        //device_two: value.device_new,
        product: that.data.product,
        package: that.data.package,
        note: value.remark
      },
      success: function (res) {
        app.showToast(res.data.message, 'none', 2000, function () {
          if (res.data.code == 1000) {
            console.log('res',res);
            //更新合同
            app.ajax({
              url: 'Common/Esignc/againPdf',
              data: {
                contract_id: res.data.data.contract_id
              },
              success: function (res1) {
                //app.showToast(res1.data.message);
                setTimeout(() => {
                  wx.navigateBack();
                }, 1000)
              }
            })

          }
        })
      }
    })
  },
  //表单重置
  formReset() {
    this.setData({
      device_old: '',//旧缴费器号
      user_name: '',//用户姓名
      user_tel: '',//用户电话
      device_new: '',//新缴费器号
      product: '',//新缴费器下发产品id
      product_name: '',
      package: '',//新缴费器下发套餐id
      package_name: '',
      note: '',//备注
    })
  },
})
